const model = [
  {
    name: 'nightLightEndTime',
    length: 1,
    code: 8
  },
  {
    name: 'nightLightStartTime',
    length: 1,
    code: 7
  },
  {
    name: 'airChangeGear',
    length: 1,
    code: 4
  },
  {
    name: 'nightLight',
    length: 1,
    code: 6
  },
  {
    name: 'windGear',
    length: 1,
    code: 3
  },
  {
    name: 'illumination',
    length: 2,
    code: 1
  },
  {
    name: 'mode',
    length: 1,
    code: 2
  },
  {
    name: 'angleAutoOnOff',
    length: 1,
    code: 9
  },
  {
    name: 'lefttime',
    length: 1,
    code: 16
  },
  {
    name: 'heatingGear',
    length: 1,
    code: 5
  }
]

/**
 * 将Alink协议的数据转换为设备能识别的格式数据，物联网平台给设备下发数据时调用
 * 下发
 * 出参：rawData byte[]数组，不能为空
 * 入参：jsonObj 对象，不能为空
 * example: {"method":"thing.service.property.set","id":"12345","version":"1.0","params":{"mode":332}}
 */
export const protocolToRawData = jsonObj => {
  // console.log(jsonObj, 'jsonObj')
  let method = jsonObj['method']
  // let id = jsonObj['id']
  // let version = jsonObj['version']
  let payloadArray = []
  let len = 0
  if (method === 'thing.service.property.set') {
    let params = jsonObj['params']
    // console.log(params, 'params==')
    for (let i = 0; i < model.length; i++) {
      for (let key in params) {
        if (model[i].name === key) {
          payloadArray[len] = 0
          payloadArray[len + 1] = model[i].code
          payloadArray[len + 2] = model[i].length
          if (model[i].length == 1) {
            let val = 0
            if (model[i].code == 2) {
              // 模式转换
              switch (params[key]) {
                case 332: // 待机
                  val = 1
                  break
                case 356: // 吹风
                  val = 2
                  break
                case 360: // 干燥
                  val = 16
                  break
                case 474: // 换气
                  val = 4
                  break
                case 486: // 取暖
                  val = 8
                  break
                case 359: // 沐浴
                  val = 32
                  break
                case 455: // 换气强暖 = 换气 + 取暖
                  val = 12
                  break
                case 789: // 智能换气
                  val = 6
                  break
                default:
                  break
              }
              payloadArray[len + 3] = val
            } else if (model[i].code === 4) {
              // 换气档位
              switch (params[key]) {
                case 4:
                  val = 1
                  break
                case 5:
                  val = 2
                  break
                case 6:
                  val = 3
                  break
                default:
                  break
              }

              payloadArray[len + 3] = val
            } else {
              payloadArray[len + 3] = params[key]
            }
          } else if (model[i].length == 2) {
            payloadArray[len + 4] = 0
            payloadArray[len + 3] = params[key]
          } else {
            console.info(`Invalid model length: ${model[i]}!`)
          }

          len = len + model[i].length + 3
        }
      }
    }

    payloadArray.unshift(payloadArray.length)
    payloadArray.unshift(2)
    let checkCode = 0
    for (let i = 0; i < payloadArray.length; i++) {
      checkCode += payloadArray[i]
    }
    checkCode = checkCode % 256
    payloadArray.push(checkCode)
    payloadArray.unshift(241)
    payloadArray.unshift(241)
    payloadArray.push(126)

    return new Buffer.from(payloadArray)
  } else if (method === 'thing.event.property.post') {
    payloadArray.unshift(payloadArray.length)
    payloadArray.unshift(0xf3)
    let checkCode = 0
    for (let i = 0; i < payloadArray.length; i++) {
      checkCode += payloadArray[i]
    }
    checkCode = checkCode % 256
    payloadArray.push(checkCode)
    payloadArray.unshift(241)
    payloadArray.unshift(241)
    payloadArray.push(126)
    return new Buffer.from(payloadArray)
  }
}

/**
 * 将设备的自定义格式数据转换为Alink协议的数据，设备上报数据到物联网平台时调用
 * 上报
 * 入参：rawData byte[]数组，不能为空
 * 出参：jsonObj 对象，不能为空
 * example: rawDataToProtocol(['f2','f2','02','06','00','08','03','30','30','30','f1','7e'])
 */
export const rawDataToProtocol = rawData => {
  let len = 3
  let jsonObj = {}
  let params = {}
  let uint8Array = new Uint8Array(rawData.length)
  for (var i = 0; i < rawData.length; i++) {
    uint8Array[i] = rawData[i] & 0xff
  }
  // console.log(uint8Array)
  for (let i = 3; i < uint8Array.length - 2; i++) {
    if (len >= uint8Array.length - 2) {
      break
    }
    for (let j = 0; j < model.length; j++) {
      // console.log(i, len, uint8Array.length)
      if (uint8Array[len + 2] === model[j].code) {
        let rawVal = uint8Array[len + 4]
        let val = rawVal
        if (model[j].code === 2) {
          switch (rawVal) {
            case 1: // 待机
              val = 332
              break
            case 2: // 吹风
              val = 356
              break
            case 4: // 换气
              val = 474
              break
            case 8: // 取暖
              val = 486
              break
            case 16: // 干燥
              val = 360
              break
            case 32: // 沐浴
              val = 359
              break
            case 12: // 换气强暖 = 换气 + 取暖
              val = 455
              break
            case 6: // 智能换气 = 换气 + 吹风
              val = 789
              break
            default:
              break
          }
          params[model[j].name] = val
        } else if (model[j].code === 4) {
          switch (rawVal) {
            case 1:
              val = 4
              break
            case 2:
              val = 5
              break
            case 3:
              val = 6
              break
            default:
              break
          }
          params[model[j].name] = val
        } else {
          val = rawVal
          if (model[j].length == 1) {
            params[model[j].name] = val
          } else if (model[j].length == 2) {
            params[model[j].name] =
              uint8Array[len + 5] * 256 + uint8Array[len + 4]
          }
        }
        // params[model[j].name] = val;
        len = len + model[j].length + 3
      }
    }
  }

  jsonObj['method'] = 'thing.event.property.post'
  jsonObj['version'] = '1.0'
  jsonObj['id'] = new Date().getTime()
  jsonObj['params'] = params
  // console.log(jsonObj)
  return jsonObj
}
